package com.tom.service.impl;


import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.DomSerializer;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class JsonpathTestImpl {

	/**
	 * 
	 * @author hanpengda
	 * @date 2018年5月21日
	 * @TODO j-Xpath测试
	 */
	public static void work() {
		String url = "https://www.huzhudai.cc/ajax/huzhudai_3rd.MyLend,huzhudai_3rd.ashx?_method=factorSearch&_session=no";
		String html = null;
		String exp = "//div[@class='activityBid']//li[@class='bidSynopsis']/ul/li//dl[@class='bidName']/dd";
        try {  
            Connection connect = Jsoup.connect(url);  
            html = connect.get().body().html();
            //System.out.println(html);
            HtmlCleaner hc = new HtmlCleaner();  
            TagNode tn = hc.clean(html); 
            Document dom = new DomSerializer(new CleanerProperties()).createDOM(tn);  
            XPath xPath = XPathFactory.newInstance().newXPath();  
            Object result; 
            result = xPath.evaluate(exp, dom, XPathConstants.NODESET);
            //System.out.println(result);
            if (result instanceof NodeList) {
            	 NodeList nodeList = (NodeList) result;  
                 //System.out.println(nodeList.getLength()); 
                 for (int i = 0; i < nodeList.getLength(); i++) {
                	 Node node = nodeList.item(i); 
                	 System.out.println(node.getAttributes().getNamedItem("onclick"));
				}
			}
        } catch (Exception e) {  
            e.printStackTrace();  
        }
      
	}
}

